package com.spark.tags;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.spark.core.SparkContext;
import com.spark.core.XMap;
import com.spark.util.CookieHelper;

public class ViewComponent extends Component {
	public static final String CLASSNAME_DISABLED = "s_dbd";
	
	public static final String SPARK_COOKIE_WIDTH = "_spw";
	public static final String SPARK_COOKIE_HEIGHT = "_sph";
	
	public static final String PROPERTITY_WIDTH = "width";
	public static final String PROPERTITY_HEIGHT = "height";
	public static final String PROPERTITY_CLASSNAME = "className";
	public static final String PROPERTITY_PROMPT = "prompt";
	public static final String PROPERTITY_STYLE = "style";
	public static final String PROPERTITY_MARGIN_WIDTH = "marginWidth";
	public static final String PROPERTITY_MARGIN_HEIGHT = "marginHeight";
	public static final String PROPERTITY_DISABLED = "disabled";
	public static final String PROPERTITY_READONLY = "readOnly";
	public static final String PROPERTITY_TABINDEX = "tabIndex";
	
//	protected String getDefaultClass(XMap view,SparkContext context){
//		String clz = super.getDefaultClass(view, context);		
//		boolean disabled = view.getBoolean(PROPERTITY_DISABLED, false);
//		if(disabled) {
//			clz.concat(" ").concat(CLASSNAME_DISABLED);
//		}
//		return clz;
//	}
	
	protected String getDefaultClass(XMap view,SparkContext context){
		StringBuilder sb = new StringBuilder(super.getDefaultClass(view, context));		
		boolean disabled = view.getBoolean(PROPERTITY_DISABLED, false);
		if(disabled) {
			sb.append(" ").append(CLASSNAME_DISABLED);
		}
		return sb.toString();
	}
	
	protected int getDefaultWidth(){
		return 150;
	}
	
	protected int getDefaultHeight(){
		return 25;
	}
	
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		/***************** Width属性 **************/
		int width = getComponentWidth(view,context);
		context.put(PROPERTITY_WIDTH, width);
		context.addJsonConfig(PROPERTITY_WIDTH,width);
		
		
		/***************** Height属性 ************/
		int height = getComponentHeight(view, context);
		if(height !=0) {
			context.put(PROPERTITY_HEIGHT, height);
			context.addJsonConfig(PROPERTITY_HEIGHT,height);
		}
		

		/***************** Style属性 *****************/
		String style = view.getString(PROPERTITY_STYLE, "");
		context.put(PROPERTITY_STYLE, style);
		

		/***************** ClassName属性 **********/
		String clazz = getDefaultClass(view, context);
		String className = view.getString(PROPERTITY_CLASSNAME, "");
		if(!"".equals(className)) {
			clazz += " " + className;
		}
		context.put(PROPERTITY_CLASSNAME, clazz);
		
		
		/*************** disabled属性 *************/		
		boolean disabled = view.getBoolean(PROPERTITY_DISABLED, false);
		context.put(PROPERTITY_DISABLED, disabled);
		if(disabled) {
			context.addJsonConfig(PROPERTITY_DISABLED,disabled);
			context.put(PROPERTITY_READONLY,"readonly=\"readonly\"");
		}
//		context.put(PROPERTITY_READONLY,"readonly="+ (disabled ? "true" : "false"));
		
		
		/*************** tabIndex属性 *************/
		int tabindex = view.getInt(PROPERTITY_TABINDEX, 0);
		context.put(PROPERTITY_TABINDEX,tabindex);
		
		filterJsonConfig(context);
	}
	
	protected void filterJsonConfig(SparkContext context){}
	
	private int getComponentSize(XMap view,SparkContext context,String cookieKey, String sizeKey, int defaultSize, String marginkey) {

		HttpServletRequest request = context.getRequest();
		String vwh = request.getParameter(cookieKey);
		if(vwh == null){
			vwh = CookieHelper.getCookieValue(context.getRequest(), cookieKey);
		}
		String ws = view.getString(sizeKey, ""+defaultSize);
		int size = "".equals(ws) ? getDefaultWidth() : Integer.parseInt(ws);
		Integer marginSize = view.getInt(marginkey);
		if(marginSize!=null&&vwh!=null) {
			int vsize = Integer.parseInt(vwh);
			size = new Integer((vsize - marginSize.intValue()) > size ? (vsize - marginSize.intValue()) :  size);
			context.addJsonConfig(marginkey, marginSize);
		}
		return size;
	}
	
	protected int getComponentWidth(XMap view,SparkContext context) {
		return getComponentSize(view,context,SPARK_COOKIE_WIDTH,PROPERTITY_WIDTH,getDefaultWidth(),PROPERTITY_MARGIN_WIDTH);
	}
	
	protected int getComponentHeight(XMap view,SparkContext context) {
		return getComponentSize(view,context,SPARK_COOKIE_HEIGHT,PROPERTITY_HEIGHT,getDefaultHeight(),PROPERTITY_MARGIN_HEIGHT);
	}
	
	protected String getFieldPrompt(XMap field,SparkContext context){
		String dataSet = field.getString(PROPERTITY_BINDTARGET);
		return getFieldPrompt(field,context,dataSet);
	}
	
	@SuppressWarnings("unchecked")
	protected String getFieldPrompt(XMap field,SparkContext context,String dataSet){
		String label = field.getString(PROPERTITY_PROMPT);
		if(label==null){
			String name = field.getString(PROPERTITY_NAME, "");
			XMap ds = context.getDataSet(dataSet);
			if(ds!=null){
				XMap fieldcm = ds.getChild(DataSet.PROPERTITY_FIELDS);
				if(fieldcm !=null){
					List fields = fieldcm.getChilds();
					Iterator it = fields.iterator();
					while(it.hasNext()){
						XMap fieldMap = (XMap)it.next();
						String fn = fieldMap.getString(PROPERTITY_NAME,"");
						if(name.equals(fn)){
							label = fieldMap.getString(PROPERTITY_PROMPT);
							break;
						}
					}
				}
			}
		}
		return label;
	}
}
